<?php

class UserGroup extends CActiveRecord {
    /**
     * The followings are the available columns in table 'www_user_groups':
     * @var integer $id
     * @var integer $type
     * @var string $name
     * @var integer $default_inteqral_value
     * @var integer $default_gold_coin_value
     * @var integer $status
     * @var integer $created
     * @var integer $updated
     */

    /**
     * Returns the static model of the specified AR class.
     * @return CActiveRecord the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{user_groups}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
        return array(
            array('type, name, default_inteqral_value, default_gold_coin_value, status', 'required'),
            array('type, default_inteqral_value, default_gold_coin_value, status, created, updated', 'numerical', 'integerOnly'=>true),
            array('name', 'length', 'max'=>60),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
        return array(
            'users' => array(self::HAS_MANY, 'User', 'group_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
        'id' => 'Id',
        'type' => Yii::t('userGroup', 'type'),
        'name' => Yii::t('userGroup', 'name'),
        'default_inteqral_value' => Yii::t('userGroup', 'default_inteqral_value'),
        'default_gold_coin_value' => Yii::t('userGroup', 'default_gold_coin_value'),
        'status' => Yii::t('userGroup', 'status'),
        'created' => Yii::t('common', 'created'),
        'updated' => Yii::t('common', 'updated'),
        );
    }

     // 状态选项
    const STATUS_DISABLED = 0;
    const STATUS_ENABLED = 1;
    public function getStatusOptions() {
        return array(
            self::STATUS_DISABLED => Yii::t('usergroup', 'status_disabled'),
            self::STATUS_ENABLED  => Yii::t('usergroup', 'status_enabled'),
        );
    }

    public function getStatusTextFormat() {
        $res = $this->getStatusOptions();
        return (isset($res[$this->status])) ? $res[$this->status] : 'unknow';
    }

    // 会员类型选项
    const TYPE_SYSTEM_USER = 0; // 系统用户
    const TYPE_REGISTER_MEMBER = 1; // 注册会员
    public function getTypeOptions() {
        return array(
            self::TYPE_SYSTEM_USER => Yii::t('usergroup', 'type_system_user'),
            self::TYPE_REGISTER_MEMBER  => Yii::t('usergroup', 'type_register_member'),
        );
    }

    // 类型（文字格式）
    public function getTypeTextFormat() {
        $res = $this->getTypeOptions();
        return (isset($res[$this->type])) ? $res[$this->type] : 'unknow';
    }

    public function getTypeHtmlFormat() {
        switch ($this->type) {
            case 0: $style = "red bold"; break;
            case 1: $style = "green bold"; break;
            default: $style = "black";
        }
        return "<font class=\"{$style}\">{$this->TypeTextFormat}</font>";
    }

    public function getUpdatedFormatted() {
        return ($this->updated) ? date('Y-m-d', $this->updated) : '';
    }

    public function behaviors() {
        return array(
            'CTimestampBehavior' => array(
                'class' => 'zii.behaviors.CTimestampBehavior',
                'createAttribute' => 'created',
                'updateAttribute' => 'updated',
            )
        );
    }
}